﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Helix_Network_Supervisor_3
{
    class FileExplorer
    {
        #region Fields

        private TreeNode rootNode;

        #endregion Fields

        #region Public Methods

        /// <summary>
        /// Populate a Tree View with Folders on the Hard Drive
        /// </summary>
        /// <param name="dirTreeView"></param>
        /// <returns></returns>
        public TreeNode PopulateTreeView(TreeView dirTreeView)
        {
            dirTreeView.Nodes.Clear();
            rootNode = new TreeNode("My Computer", 0, 0);
            dirTreeView.Nodes.Add(rootNode);

            TreeNodeCollection driveCollection = rootNode.Nodes;

            DriveInfo[] availableDrives = DriveInfo.GetDrives();
            string currentDrive = "";

            foreach (DriveInfo d in availableDrives)
            {
                currentDrive = d.Name;

                int iImageIndex = 0, iSelectedIndex = 0;
                if (d.DriveType == DriveType.Fixed)
                {
                    iImageIndex = 1;
                    iSelectedIndex = 1;
                } // End if
                else if (d.DriveType == DriveType.CDRom)
                {
                    iImageIndex = 2;
                    iSelectedIndex = 2;
                } // End else if
                else
                {
                    iImageIndex = 3;
                    iSelectedIndex = 3;
                } // End else

                TreeNode driveNode = new TreeNode(currentDrive, iImageIndex, iSelectedIndex);

                driveCollection.Add(driveNode);

                driveNode.Nodes.Add("");
            } // End foreach
            return rootNode;
        } // End PopulateTreeView()

        /// <summary>
        /// Populates a Tree Node with the Contents of the Selected Folder
        /// </summary>
        /// <param name="currentDrive"></param>
        /// <param name="tcCollection"></param>
        /// <returns></returns>
        public int PopulateFolders(string currentDrive, TreeNodeCollection tcCollection)
        {
            if (Directory.Exists(currentDrive) == false)
            {
                MessageBox.Show("Directory or path " + currentDrive + " doesn't exist",
                    "Helix Show Builder - Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                //this.Cursor = Cursors.Arrow;
                //StatusLabel.Text = "Select the folder that contains the Vixen v2 Audio Files";
                return 0;
            } // End if

            try
            {
                DirectoryInfo di = new DirectoryInfo(currentDrive);
                string dirFullPath = "", dirName = "";
                int iImageIndex = 4, iSelectedIndex = 5;

                DirectoryInfo[] diArr = di.GetDirectories();

                foreach (DirectoryInfo tempDirInfo in diArr)
                {
                    if ((tempDirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        continue;
                    } // End if
                    else
                    {
                        dirFullPath = tempDirInfo.FullName;
                        dirName = tempDirInfo.Name;

                        TreeNode currNode = new TreeNode(dirName, iImageIndex, iSelectedIndex);
                        tcCollection.Add(currNode);

                        currNode.Nodes.Add("");
                    } // End else
                } // End foreach
            } // End try
            catch
            {
                //this.Cursor = Cursors.Arrow;
                //StatusLabel.Text = "Select the folder that contains the Vixen v2 Audio Files";
                return 0;
            } // End catch

            return 1;
        } // End PopulateFolders()

        #endregion Public Methods
    }
}
